import Vuex from 'vuex'
import Vue from 'vue'
import * as types from './types'
import * as mycookie from './cookieutils'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    user: null,
    token: null,
    title: '',
    link1: '',
    link_hq: '',
    link_hz: '',
    link_hh: '',
    pwdtimestamp: '',
    Roles: '',
    pwdFlag: false,
    unitName: '',
    showIdleWarning: false
  },
  mutations: {
    SET_IDLE_WARNING (state, value) {
      state.showIdleWarning = value
    },
    [types.LOGIN]: (state, params) => {
      localStorage.setItem('token', params.data)
      // state.token = params.data
      localStorage.setItem('user', params.user)
      // state.user = params.user
      localStorage.setItem('Roles', params.Roles)
      // state.Roles = params.Roles
      localStorage.setItem('pwdFlag', params.pwdFlag)
      // state.pwdFlag = params.pwdFlag
      localStorage.setItem('unitName', params.unitName)
      // state.unitName = params.unitName
      localStorage.setItem('newver', '2020')
      mycookie.setCookie('loginflag', '1')
    },
    [types.LOGOUT]: state => {
      // alert('vuex方法clear')
      localStorage.clear()
      state.token = null
    },
    [types.TITLE]: (state, data) => {
      localStorage.setItem('title', data)
      // state.title = data
    },
    'tablink1': (state, data) => {
      localStorage.setItem('Link1', data.Link1)
      state.link1 = data.Link1
    },
    'tablink_hq': (state, data) => {
      localStorage.setItem('Link_hq', data.Link_hq)
      state.link_hq = data.Link_hq
    },
    'tablink_hz': (state, data) => {
      localStorage.setItem('Link_hz', data.Link_hz)
      state.link_hz = data.Link_hz
    },
    'tablink_hh': (state, data) => {
      localStorage.setItem('Link_hh', data.Link_hh)
      state.link_hh = data.Link_hh
    },
    'pwdtimestamp': (state, data) => {
      localStorage.setItem('pwdtimestamp', data.pwdtimestamp)
      // state.pwdtimestamp = data.pwdtimestamp
    }
  },
  actions: {
    showIdleWarning ({ commit }, value) {
      commit('SET_IDLE_WARNING', value)
    },
    async sendHeartbeat () {
      try {
        // await axios.post('/api/heartbeat')
      } catch (error) {
        console.error('Heartbeat failed', error)
      }
    },
    async logout () {
      try {
        // await axios.post('/account/logoff')
        // localStorage.clear()
        // this.commit('CLEAR_USER')
        this.commit('SET_IDLE_WARNING', false)
      } catch (error) {
        console.error('Logout failed', error)
      }
    }
  }
})
